package com.woniuxy.aopdemo1;

import org.aopalliance.intercept.MethodInvocation;
import org.aopalliance.intercept.MethodInterceptor;

import java.lang.reflect.Method;

public class MyAspect implements MethodInterceptor{
    //定义通知: 前置通知
    public void checkPermission(){
        System.out.println("前置通知");
    }
    //定义后置通知
    public void commitThransaction(){
        System.out.println("后置通知");
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        checkPermission();
        if (invocation.getMethod().getName().startsWith("add")) {
            System.out.println("是新增方法！");
        }else if (invocation.getMethod().getName().startsWith("select")){
            System.out.println("是查询方法");
        }else if (invocation.getMethod().getName().startsWith("updata")){
            System.out.println("是修改方法");
        }else if (invocation.getMethod().getName().startsWith("delete")){
            System.out.println("是删除方法");
        }
        //核心还是用的反射
        Object proceed = invocation.proceed();
        commitThransaction();
        return proceed;
    }


}
